
#include "log/Logger.h"
#include <mutex>
#include <iostream>
#include <string>

std::mutex g_LogManagerMtx;

CLogger* CLogger::m_pInstance = nullptr;

CLogger* CLogger::instance()
{
    if(nullptr == m_pInstance)
    {
        g_LogManagerMtx.lock();
        if(nullptr == m_pInstance)
        {
            m_pInstance = new CLogger();
        }
        g_LogManagerMtx.unlock();
        return m_pInstance;
    }
    else 
    {
        return  m_pInstance;
    }
}

CLogger::CLogger()
{
    // 加载配置文件
    // 需要能够动态调整打印等级
    try
    {
      log4cpp::PropertyConfigurator::configure("./CFG/log4cfg");
    }
    catch (log4cpp::ConfigureFailure & f)
    {
        std::cerr << "configure problem " << f.what() << std::endl;
    }
    if (log4cpp::Category::exists("log"))
    {
        std::cout << "Category::exists " << "log" << std::endl;
    }
    else
    {
        std::cerr << "Category::exists error " << "log" << std::endl;
    }
    if (log4cpp::Category::exists("dataCapture1"))
    {
        std::cout << "Category::exists " << "dataCapture1" << std::endl;
    }
    else
    {
        std::cerr << "Category::exists error " << "dataCapture1" << std::endl;
    }
}

CLogger::~CLogger()
{
    log4cpp::Category& log = log4cpp::Category::getRoot();
    log.shutdown();
}

void CLogger::logDebug(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.debug(strMsg);
}

void CLogger::logError(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.error(strMsg);
}

void CLogger::logInfo(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.info(strMsg);
}

void CLogger::logWarn(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.warn(strMsg);
}

void CLogger::logFatal(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("log");
    log.fatal(strMsg);
}

void CLogger::dataCapture1(const std::string& strMsg)
{
    log4cpp::Category& log = log4cpp::Category::getInstance("dataCapture1");
    log.info(strMsg);
}
